﻿using System;
using System.Data.SQLite;
using System.IO;
using System.Windows.Input;
using Monti.MVVM;

namespace SqliteHelper.Classes
{
   /// <summary>
   /// View model for the new database dialog.
   /// </summary>
   public class NewDatabaseViewModel : DialogBase
   {
      private bool m_BoolUtf8;
      private string m_TxtPath;
      private RelayCommand m_CmdBrowse;

      /// <summary>
      /// Initializes a new database object.
      /// </summary>
      public NewDatabaseViewModel ()
      {
         this.IsUtf8 = true;
         this.Width = 300;
         this.Height = 190;
      }

      /// <summary>
      /// Gets or Sets the new database path.
      /// </summary>
      public string DatabasePath
      {
         get { return this.m_TxtPath; }
         set
         {
            this.m_TxtPath = value;
            this.RaisePropertyChanged ("DatabasePath");
         }
      }

      /// <summary>
      /// Gets or Sets if the database should be created using UTF-8.
      /// </summary>
      public bool IsUtf8
      {
         get { return this.m_BoolUtf8; }
         set
         {
            this.m_BoolUtf8 = value;
            this.RaisePropertyChanged ("IsUtf8");
         }
      }

      /// <summary>
      /// Gets the connection string.
      /// </summary>
      protected string ConnectionString
      {
         get
         {
            return string.Format ("Data Source={0};Version=3;{1}", this.DatabasePath,
               (this.IsUtf8) ? string.Empty : "UseUTF16Encoding=True;");
         }
      }

      /// <summary>
      /// Gets the command for browsing to a path.
      /// </summary>
      public ICommand BrowseCommand
      {
         get { return this.m_CmdBrowse ?? (this.m_CmdBrowse = new RelayCommand (Browse)); }
      }

      /// <summary>
      /// Shows the browse dialog.
      /// </summary>
      /// <param name="obj">Command Parameter.</param>
      private void Browse (object obj)
      {
         var fileService = DialogService.Instance.GetService<FileDialogService> ();
         var viewModel = new FileDialogViewModel { AllowMultipleSelection = false, Filter = "Database files (*.db)|*.db|All files(*.*)|*.*" };
         bool? result = fileService.SaveFile (viewModel);
         if (result.HasValue && result.Value)
         {
            this.DatabasePath = viewModel.FileName;
            if (File.Exists (this.DatabasePath))
               this.ErrorText = "Selected files exits! This file will be deleted first.";
         }
      }

      /// <summary>
      /// Occurs when the ok button on the dialog is pressed.
      /// </summary>
      public override void OkExecuted ()
      {
         this.CheckExistingFile ();
         if (this.OkToClose)
         {
            using (var connection = new SQLiteConnection (this.ConnectionString))
            {
               connection.Open ();
               connection.Close ();
            }
         }
      }

      /// <summary>
      /// Checks if the ok command can execute.
      /// </summary>
      /// <returns>If it is ok to execute.</returns>
      public override bool CanOkExecute ()
      {
         return !string.IsNullOrEmpty (this.DatabasePath);
      }

      /// <summary>
      /// Check if there is an existing file.
      /// </summary>
      private void CheckExistingFile ()
      {
         this.OkToClose = true;
         if (File.Exists (this.DatabasePath))
         {
            try
            {
               File.Delete (this.DatabasePath);
            }
            catch (Exception)
            {
               this.ErrorText = "Unable to delete existing file!";
               this.OkToClose = false;
            }
         }
      }
   }
}
